import programApi from 'api/program'
// state
let state = {
  classList: {},
  details: {},
  periodList: {},
  plan: {},
  planDetails: {}
}

// actions
const actions = {
  classList ({ commit, state }, params) { // 获取列表
    return programApi.classList(params).then((json) => {
      commit('CLASSLIST', {
        params: params,
        data: json.data
      })
    })
  },
  details ({ commit, state }, params) { // 获取大纲详情
    return programApi.detail(params).then((json) => {
      commit('DETAILS', {
        params: params,
        data: json.data
      })
    })
  },
  periodList ({ commit, state }, params) { // 获取课时列表
    return programApi.periodList(params).then((json) => {
      commit('PERIODLIST', {
        params: params,
        data: json.data
      })
    })
  },
  plan ({ commit, state }, params) { // 获取训练计划
    return programApi.plan(params).then((json) => {
      commit('PLAN', {
        params: params,
        data: json.data
      })
    })
  },
  planDetails ({ commit, state }, params) { // 获取训练方式
    return programApi.planDetails(params).then((json) => {
      commit('PLANDETAILS', {
        params: params,
        data: json.data
      })
    })
  }
}
// mutations
const mutations = {
  CLASSLIST (state, { data }) {
    state.classList = data
  },
  DETAILS (state, {data}) {
    state.details = data
  },
  PERIODLIST (state, {data}) {
    if (data && data.length > 0) {
      data.forEach(item => {
        item.time = new Date(item.beginDate).format('yyyy-MM-dd ') + new Date(item.startTime).format('HH:mm') + '-' + new Date(item.endTime).format('HH:mm')
      })
    }
    state.periodList = data
  },
  PLAN (state, {data}) {
    data.classTime = new Date(data.classDate).format('yyyy-MM-dd ') + new Date(data.beginTime).format('HH:mm-') + new Date(data.endTime).format('HH:mm')
    state.plan = data
  },
  PLANDETAILS (state, {data}) {
    state.planDetails = data
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
